package com.qf.es.service;

import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import utils.ESClient;

import java.io.IOException;

public class DocService {
    private static final String INDEX_NAME = "gmt";

    public void addDoc() throws IOException {
        RestHighLevelClient restHighLevelClient = ESClient.getClient();
        IndexRequest indexRequest = new IndexRequest(INDEX_NAME).id("2");
        String json = "{\n" +
                "  \"corpName\":\"盒马鲜生1\",\n" +
                "  \"mobile\":\"13100000000\",\n" +
                "  \"createDate\":\"2020-02-18 19:19:20\",\n" +
                "  \"sendDate\":\"2020-02-18 19:19:21\",\n" +
                "  \"longCode\":\"10660000988\",\n" +
                "  \"smsContent\":\"【盒马1】您尾号12345678的订单已开始配送，请在您指定的时间收货不要走开哦~配送员：刘三，电话：13800000000\",\n" +
                "  \"state\":0,\n" +
                "  \"province\":\"北京\",\n" +
                "  \"operatorId\":2,\n" +
                "  \"ipAddr\":\"10.126.2.9\",\n" +
                "  \"replyTotal\":15,\n" +
                "  \"fee\":5\n" +
                "}";
        indexRequest.source(json, XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        System.err.println(response);
    }

    public void addDocNoId() throws IOException {
        RestHighLevelClient restHighLevelClient = ESClient.getClient();//获取连接
        IndexRequest indexRequest = new IndexRequest(INDEX_NAME);//自动生成主键
        String json = "{\n" +
                "  \"corpName\":\"盒马鲜生\",\n" +
                "  \"mobile\":\"13100000000\",\n" +
                "  \"createDate\":\"2020-02-18 19:19:20\",\n" +
                "  \"sendDate\":\"2020-02-18 19:19:21\",\n" +
                "  \"longCode\":\"10660000988\",\n" +
                "  \"smsContent\":\"【盒马】您尾号12345678的订单已开始配送，请在您指定的时间收货不要走开哦~配送员：刘三，电话：13800000000\",\n" +
                "  \"state\":0,\n" +
                "  \"province\":\"北京\",\n" +
                "  \"operatorId\":2,\n" +
                "  \"ipAddr\":\"10.126.2.9\",\n" +
                "  \"replyTotal\":15,\n" +
                "  \"fee\":5\n" +
                "}";
        indexRequest.source(json, XContentType.JSON);//传递数据
        IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        System.err.println(response);
    }

    public void updateDoc() throws IOException {
        RestHighLevelClient restHighLevelClient = ESClient.getClient();
        String json = "{\n" +
                "  \"corpName\":\"盒马鲜生2222222\"\n" +
                "}";
        UpdateRequest request = new UpdateRequest(INDEX_NAME,"2").doc(json,XContentType.JSON);
        UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
        System.err.println(response);
    }

    public void deleteDoc() throws IOException {
    RestHighLevelClient restHighLevelClient = ESClient.getClient();
        DeleteRequest deleteRequest = new DeleteRequest(INDEX_NAME,"2");
        DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        System.err.println(response);
    }

    public SearchResponse processSearch(QueryBuilder builder) throws IOException {
        RestHighLevelClient restHighLevelClient = ESClient.getClient();//获取连接
        SearchRequest request = new SearchRequest(INDEX_NAME);//设置查询什么库
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(builder);//设置查询的方式为 term 查询
        request.source(sourceBuilder);//给请求设置查询的 builder
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        System.err.println(response);
        return response;
    }

    public void termQuery(String field, String value) throws IOException {
        processSearch(QueryBuilders.termQuery(field, value));
    }

    public void termsQuery(String field,String ... value) throws IOException {
        processSearch(QueryBuilders.termsQuery(field, value));
    }

    public void matchAll() throws IOException {
        processSearch(QueryBuilders.matchAllQuery());
    }

    public void match(String field, String value) throws IOException {
        processSearch(QueryBuilders.matchQuery(field, value));
    }

    public void bool_match(String field, String value, Operator operator) throws IOException {
        processSearch(QueryBuilders.matchQuery(field, value).operator(operator));
    }

}
